package cn.edu.lingnan.springboot.config;


import cn.edu.lingnan.springboot.component.LoginHandlerInterceptor;
import cn.edu.lingnan.springboot.component.MyLocaleResolver;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
//@EnableWebMvc 全面接管SpringMvc
public class MyMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/index.html").setViewName("login");
        registry.addViewController("/login.html").setViewName("login");
        registry.addViewController("main.html").setViewName("dashboard");
    }

    //向容器中注册一个拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        /**对每一个路径都进行拦截,除了一部分，如下
            registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
//          表明以下的资源不进行登录拦截
            .excludePathPatterns("/index.html","/","/user/login","/webjars/**","/asserts/**");

//           静态资源
//          SpringBoot1.0已经做好了静态资源映射，所以可以不进行放行
//          但是SpringBoot就是需要进行一个静态资源的放行————"/webjars/**","/asserts/**"
    }

    //    方法名一定要是localeResolver
//    一定要将组建注册到容器中
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }









    //    @Bean 在2.0中WebMvcConfigurationAdapter已经被弃用了
//    public WebMvcConfigurationAdapter webMvConfigurationAdapter(){
//        WebMvcConfigurationAdapter adapter = new WebMvcConfigurationAdapter() {
//
//            public void addViewController(ViewControllerRegistry registry){
//                registry.addViewController("/").setViewName("login");
//                registry.addViewController("/index.hthml").setViewName("login");
//            }
//        };
//        return adapter;
//    }
}
